<?php
/**
 * Model.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/25
 * Time: 11:29
 */
namespace app\console\command\auto_curd\tpl;

use app\api\util\Hump;

class Model
{
    static public function begin($dir,$className,$databaseName)
    {
        $date = date('Y/m/d',time());
        $time = date('H:i',time());
        $tNamespace = str_replace("/","\\",$dir).$databaseName;
        $tpl = <<<EOF
<?php
/**
 * $className.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: $date
 * Time: $time
 */
namespace app\\{$tNamespace}\\model;

use think\\Db;

class $className
{
EOF;
        return $tpl;
    }

    static public function content($column)
    {
        $tColumnName = Hump::camel($column['column_name']);
        $tpl = <<<EOF

    /**
     * {$column['column_comment']}
     */
    protected \${$column['column_name']} = '{$column['column_default']}';
    public function get{$tColumnName}()
    {
        return \$this->{$column['column_name']};
    }
    public function set{$tColumnName}(\${$column['column_name']})
    {
        \$this->{$column['column_name']} = \${$column['column_name']};
    }
    
EOF;
        return $tpl;
    }

    static public function construct($column)
    {
        $tpl = <<<EOF

        // {$column['column_comment']}
        if(isset(\$data['{$column['column_name']}']))
        {
            \$this->{$column['column_name']} = \$data['{$column['column_name']}'];
        }

EOF;
        return $tpl;
    }

    static public function end($construct,$tableName,$PRI_NAME)
    {
        $tpl = <<<EOF

    /**
     * constructor.
     * @param array \$data
     */
    public function __construct(\$data=[])
    {
$construct
    }
    
    /**
     * 获取分页信息
     * @param \$where
     * @param array \$extend
     * @return \\think\\Paginator
     */
    static public function paginate(\$where,\$extend=[])
    {
        \$handler = Db::table('$tableName');
        if(!empty(\$extend['field']))
        {
            \$handler->field(\$extend['field']);
        }
        if(!empty(\$extend['order']))
        {
            \$handler->order(\$extend['order']);
        }
        if(!empty(\$extend['group']))
        {
            \$handler->group(\$extend['group']);
        }
        return \$handler
            ->where(\$where)
            ->paginate();
    }
    
    /**
     * 添加|修改
     * @param \$flag
     * @param \$data
     * @param array \$extend
     * @return int|string
     */
    static public function save(\$flag,\$data,\$extend=[])
    {
        if(empty(\$flag))
        {
            // 添加
            \$data['create_ts'] = time();
            return Db::table('$tableName')
                ->insertGetId(\$data);
        }
        else
        {
            // 编辑
            \$data['update_ts'] = time();
            return Db::table('$tableName')
                ->where('$PRI_NAME',\$flag)
                ->update(\$data);
        }
    }
    
    /**
     * 获取详情
     * @param \$where
     * @param array \$extend
     * @return array|false|null|\PDOStatement|string|\\think\Model
     */
    static public function get(\$where,\$extend=[])
    {
        \$handler = Db::table('$tableName');
        if(!empty(\$extend['field']))
        {
            \$handler->field(\$extend['field']);
        }
        if(!empty(\$extend['order']))
        {
            \$handler->order(\$extend['order']);
        }
        if(!empty(\$extend['group']))
        {
            \$handler->group(\$extend['group']);
        }
        return \$handler
            ->where(\$where)
            ->find();
    }
    
    /**
     * 删除
     * @param \$flag
     * @param array \$extend
     * @return int
     */
    static public function delete(\$flag,\$extend=[])
    {
        return Db::table('$tableName')
            ->where('$PRI_NAME',\$flag)
            ->delete();
    }
}
EOF;
        return $tpl;
    }
}